<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/12/5
 * Time: 14:24
 */

namespace app\api\controller;


use think\Db;

class Address extends Base
{
    private $user_id;
    private $page;
    private $create_time;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    
    /**
     * 收货地址列表
     * @return \think\response\Json
     */
    public function addrList(){
        $addr = Db::table('up_my_address')
            ->join('up_area ua1', 'ua1.district_id = addr_sheng', 'left')
            ->join('up_area ua2', 'ua2.district_id = addr_shi', 'left')
            ->join('up_area ua3', 'ua3.district_id = addr_qu', 'left')
            ->field('addr_id, addr_user_name, addr_phone, ua1.district as province, 
            ua2.district as city, ua3.district, addr_address, addr_is_default')
            ->where('addr_user_id', $this->user_id)
            ->order('addr_is_default asc, addr_time desc')
            ->select();
        if($addr){
            return json(['status' => 200, 'msg' => $addr]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 新增收货地址
     * @return \think\response\Json
     * addr_user_name      收货人姓名
     * addr_address        收货详细地址
     * addr_sheng          省
     * addr_shi            市
     * addr_qu             区
     * phone               手机
     * default_addr        是否是默认地址   1 默认地址   2 非默认地址
     */
    public function addAddr(){
        $params = input('');
        $insert = [
            'addr_user_id' => $this->user_id,
            'addr_user_name' => $params['addr_user_name'],
            'addr_phone' => $params['phone'],
            'addr_address' => $params['addr_address'],
            'addr_sheng' => $params['addr_sheng'],
            'addr_shi' => $params['addr_shi'],
            'addr_qu' => $params['addr_qu'],
            'addr_is_default' => $params['default_addr'],
            'addr_time' => $this->create_time,
            'addr_update_time' => $this->create_time
        ];
        if($params['default_addr'] == 1){
            // 先将所有收货地址设置成非默认
            Db::table('up_my_address')->where('addr_user_id', $this->user_id)->setField('addr_is_default', 2);
        }
        
        $res = Db::table('up_my_address')->insert($insert);
        if($res){
            return json(['status' => 200, 'msg' => '添加成功']);
        }else{
            return json(['status' => 0, 'msg' => '添加失败']);
        }
    }
    
    /**
     * 编辑收货地址
     * @return \think\response\Json
     * addr_user_name      收货人姓名
     * addr_address        收货详细地址
     * addr_sheng          省
     * addr_shi            市
     * addr_qu             区
     * default_addr        是否是默认地址   1 默认地址   2 非默认地址
     * addr_id             ID
     * phone               手机
     */
    public function updateAddr(){
        $params = input('');
        $update = [
            'addr_user_name' => $params['addr_user_name'],
            'addr_address' => $params['addr_address'],
            'addr_sheng' => $params['addr_sheng'],
            'addr_shi' => $params['addr_shi'],
            'addr_qu' => $params['addr_qu'],
            'addr_is_default' => $params['default_addr'],
            'addr_phone' => $params['phone'],
            'addr_update_time' => $this->create_time
        ];
            
        if($params['default_addr'] == 1){
            // 先将所有收货地址设置成非默认
            Db::table('up_my_address')->where('addr_user_id', $this->user_id)->setField('addr_is_default', 2);
        }
        
        $res = Db::table('up_my_address')->where('addr_id', $params['addr_id'])->update($update);
        if($res){
            return json(['status' => 200, 'msg' => '编辑成功']);
        }else{
            return json(['status' => 0, 'msg' => '编辑失败']);
        }
    }
    
    /**
     * 删除收货地址
     * @return \think\response\Json
     * addr_id    ID
     */
    public function delAddr(){
        $addr_id = input('addr_id');
        $res = Db::table('up_my_address')->delete($addr_id);
        if($res){
            return json(['status' => 200, 'msg' => '删除成功']);
        }else{
            return json(['status' => 0, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 设置默认地址
     * @return \think\response\Json
     * addr_id     ID
     */
    public function setDefault(){
        $params = input('');
        $is_default = Db::table('up_my_address')->where('addr_id', $params['addr_id'])->value('addr_is_default');
        
        if($is_default == 2){ // 如果设置的地址不是默认地址
            Db::startTrans();
            try{
                // 先将所有收货地址设置成非默认
                Db::table('up_my_address')->where('addr_user_id', $this->user_id)->setField('addr_is_default', 2);
    
                // 设置该条记录为默认地址
                Db::table('up_my_address')->where('addr_id', $params['addr_id'])->setField('addr_is_default', 1);
                Db::commit();
                return json(['status' => 200, 'msg' => '设置成功']);
            }catch(\Exception $e){
                Db::rollback();
                return json(['status' => 0, 'msg' => '设置失败']);
            }
        }else{
            return json(['status' => 200, 'msg' => '设置成功']);
        }
    }
    
    /**
     * 默认收货地址
     * @return \think\response\Json
     */
    public function defaultAddr(){
        $addr = Db::table('up_my_address')
            ->join('up_area ua1', 'ua1.district_id = addr_sheng', 'left')
            ->join('up_area ua2', 'ua2.district_id = addr_shi', 'left')
            ->join('up_area ua3', 'ua3.district_id = addr_qu', 'left')
            ->field('addr_id, addr_user_name, addr_phone, ua1.district as province, 
            ua2.district as city, ua3.district, addr_address, addr_is_default')
            ->where('addr_user_id', $this->user_id)
            ->where('addr_is_default', 1)
            ->find();
        if($addr){
            return json(['status' => 200, 'msg' => $addr]);
        }else{
            $addr = Db::table('up_my_address')
                ->join('up_area ua1', 'ua1.district_id = addr_sheng', 'left')
                ->join('up_area ua2', 'ua2.district_id = addr_shi', 'left')
                ->join('up_area ua3', 'ua3.district_id = addr_qu', 'left')
                ->field('addr_id, addr_user_name, addr_phone, ua1.district as province, 
            ua2.district as city, ua3.district, addr_address, addr_is_default')
                ->where('addr_user_id', $this->user_id)
                ->order('addr_time desc')
                ->find();
            return json(['status' => 200, 'msg' => $addr]);
        }
    }
    
    /**
     * 选择收货地址
     * @return \think\response\Json
     * addr_id    收货地址ID
     */
    public function chooseAddr(){
        $addr_id = input('addr_id');
        $addr = Db::table('up_my_address')
            ->join('up_area ua1', 'ua1.district_id = addr_sheng', 'left')
            ->join('up_area ua2', 'ua2.district_id = addr_shi', 'left')
            ->join('up_area ua3', 'ua3.district_id = addr_qu', 'left')
            ->field('addr_id, addr_user_name, addr_phone, ua1.district as province, 
            ua2.district as city, ua3.district, addr_address, addr_is_default, addr_sheng, addr_shi, 
            addr_qu')
            ->where('addr_id', $addr_id)
            ->find();
        if($addr){
            return json(['status' => 200, 'msg' => $addr]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
}